<?php
/**
 * 文件下载控制器
 *
 * Copyright (c) 2011 tomato
 *
 * Distributed under the terms of the MIT License.
 * Redistributions of files must retain the above copyright notice.
 *
 * PHP version 5
 *
 * @author	 tomato <wangshijun2010@gmail.com>
 * @copyright	(c) 2011 tomato <wangshijun2010@gmail.com>
 * @license	http://www.opensource.org/licenses/mit-license.php The MIT License
 * @package	Media
 * @subpackage	Downloads
 */

App::uses('AppController', 'Controller');

class DownloadsController extends AppController {

	public $helpers = array('Media.Media');

	public $paginate = array(
		'conditions' => array(),
		'contain' => array('Attachment', 'Category'),
		'order' => array('id' => 'DESC'),
	);

	public function beforeFilter() {
		parent::beforeFilter();

		$this->singular = Inflector::singularize($this->plural);			// 单数, articleCategory
		$this->underscore = Inflector::underscore($this->plural);	// 复数, article_categories
		$this->camelize = Inflector::camelize($this->singular);			// 单数, ArticleCategory
		$this->humanize = Inflector::humanize($this->underscore); // 复数, Article Categories

		$this->Auth->allow(array('download', 'index'));
	}

	public function beforeRender() {
		parent::beforeRender();
		$this->set('inflector', array(
			'plural' => $this->plural,
			'singular' => $this->singular,
			'underscore' => $this->underscore,
			'camelize' => $this->camelize,
			'humanize' => $this->humanize,
		));
	}

	/**
	 * index method
	 *
	 * @return void
	 * @menu true
	 */
	public function admin_index() {
		$this->set('downloads', $this->paginate());
	}

	/**
	 * view method
	 *
	 * @param string $id
	 * @return void
	 */
	public function admin_view($id = null) {
		$this->Download->id = $id;
		if (!$this->Download->exists()) {
			throw new NotFoundException(__('Invalid download'));
		}
		$this->set('download', $this->Download->read(null, $id));
	}

	/**
	 * add method
	 *
	 * @return void
	 */
	public function admin_add() {
		if ($this->request->is('post')) {
			//debug($this->request->data); exit();
			$this->Download->create();
			if (!empty($this->request->data['Download']['publish_date'])) {
				$slice = substr(trim($this->request->data['Download']['publish_date']), 0, 10);
				$this->request->data['Download']['publish_date'] = $slice . ' 00:00:00';
			}
			$this->request->data['Download']['published'] = 1;
			if ($this->Download->saveAll($this->request->data)) {
				$this->Session->setFlash(__('The download has been saved'), 'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The download could not be saved. Please, try again.'), 'error');
			}
		}

		$author_id = $this->user('id');
		$categories = $this->{$this->modelClass}->Category->getParents();
		$this->set(compact('categories', 'author_id'));
	}

	/**
	 * edit method
	 *
	 * @param string $id
	 * @return void
	 */
	public function admin_edit($id = null) {
		$this->Download->id = $id;
		if (!$this->Download->exists()) {
			throw new NotFoundException(__('Invalid download'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if (!empty($this->request->data['Download']['publish_date'])) {
				$slice = substr(trim($this->request->data['Download']['publish_date']), 0, 10);
				$this->request->data['Download']['publish_date'] = $slice . ' 00:00:00';
			}

			if (empty($this->request->data['Attachment'][0]['file']['name'])) {
				unset($this->request->data['Attachment']);
			}

			if ($this->Download->saveAll($this->request->data)) {
				$this->Session->setFlash(__('The download has been saved'), 'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The download could not be saved. Please, try again.'), 'error');
			}
		} else {
			$this->request->data = $this->Download->read(null, $id);
		}

		$author_id = $this->user('id');
		$categories = $this->{$this->modelClass}->Category->getParents();
		$this->set(compact('categories', 'author_id'));
	}

	/**
	 * delete method
	 *
	 * @param string $id
	 * @return void
	 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}

		$this->Download->id = $id;
		if (!$this->Download->exists()) {
			throw new NotFoundException(__('Invalid download'));
		}

		$deleted = $this->Download->delete();
		if ($this->Session->read('Download.deleted') === true) {
			$this->Session->delete('Download.deleted');
			$this->Session->setFlash(__('Download deleted'), 'success');
			$this->redirect(array('action'=>'index'));
		}

		$this->Session->setFlash(__('Download was not deleted'), 'error');
		$this->redirect(array('action' => 'index'));
	}

	/**
	 * 所有人: 下载资源文件
	 */
	public function download($id = null) {
		$download = $this->Download->find('first', array(
			'conditions' => array('Download.id' => $id),
			'contain' => array('Attachment'),
		));

		if (!$download) {
			throw new NotFoundException(__('Invalid download'));
		}

		$new_count = $download['Download']['download_count'] + 1;

		$this->Download->id = $id;
		$this->Download->saveField('download_count', $new_count);

		$this->redirect(array(
			'controller' => 'attachments',
			'action' => 'download',
			'plugin' => 'media',
			$download['Attachment'][0]['id'],
		));
	}

	/**
	 * 所有人: 浏览文件列表
	 */
	public function index() {
		$category_id = $this->request->params['named']['category'];

		$this->paginate['order'] = array('Download.publish_date' => 'DESC');
		$this->paginate['conditions'] = array(
			$this->modelClass . '.category_id' => $category_id,
			$this->modelClass . '.published' => 1,
		);

		$this->{$this->modelClass}->Category->id = $category_id;
		$this->set('title', $this->{$this->modelClass}->Category->field('name'));

		$this->paginate['limit'] = 20;
		$this->paginate['contain'] = array('Attachment');

		$this->set('downloads', $this->paginate());
		$this->set('categories', $this->Download->Category->getThreaded(true));
	}

}